<?php
define('ACC', true);
require('./include/init.php');
session_start(); 
//print_r($_SESSION);
$userid = $_SESSION['user_id'];

echo '<head><meta charset = "utf-8"></head>';  
//$userid = 26;
 echo '<h2>采购&nbsp&nbsp<a href = "dashboard.php">返回虚拟公司主页</a></h2><br/>';   
 
$company = new companymodel();
$companyinfo = $company->getcompanyinfo($userid);
$companyid = $companyinfo['company_id'];
 
echo '<h2>公司名称为:' . $companyinfo['company_name'], '&nbsp|&nbsp';
echo '目前所在财年:第' . $companyinfo['company_y'] . '财年', '&nbsp|&nbsp';
echo '目前所在季度:第' . $companyinfo['company_q'] . '季度', '</h2><br/><br/><br/>';

$cashinfo = $company->getcash_equivalent($companyid);
$ttlcashequivalent = 0;
echo '<h3>现金及现金等价物：';
foreach($cashinfo as $k=>$v){ 
    if(($k == '库存现金') || ($k == '银行存款')){
        $ttlcashequivalent = $ttlcashequivalent + $v;   
    }
    echo '<' . $k . '>' . $v . '|';
}

echo '</h3>';

switch($_GET['name']){
    case 'building':
    $buildinginfo = $company->getallelements($companyid, 'building');  
$buildinglist = $company->getelementlist('building');

echo '<h4>目前可供选择的厂房有以下' . count($buildinglist) . '种：</h4>';
echo '<form name="building" action="purchaseAct.php" method="post"><table border = "1">
          <tr><td>厂房类别</td><td>厂房价格</td><td>目前已有数量</td><td>目前资金还可购入</td><td>采购数量输入</td> </tr>';
foreach($buildinglist  as $k=>$v){
    $buildingname = $v['element_name'];
    $buildingprice =  $v['building_price'];
    
    $numofbuilding = 0;
    foreach($buildinginfo as $key =>$value){
         if($value['element_id'] == $v['element_id']){
             $numofbuilding++;
         } 
    }                                                                                                                                                                           
    echo'<tr><td>' . $buildingname . '</td><td>' . $buildingprice . '</td><td>' . $numofbuilding . '</td><td>' .  floor($ttlcashequivalent / $buildingprice) .  '</td><td><input type="text" name="' . $buildingname. '" /></td></tr>' ;
}          
      
echo '</table>
<input type = "hidden" name="type" value="building">
<input type = "submit" value = "提交"></form><br/>';
break;
    
    
    case 'equipment':
    $equipmentinfo = $company->getallelements($companyid, 'equipment');  
$equipmentlist = $company->getelementlist('equipment');

echo '<h4>目前厂房内共有' .  $company->getnumofemptypositions($companyid) . '个位置可以放置生产线；可供选择的生产线有以下' . count($equipmentlist) . '种：</h4>';
echo '<form name="equipment" action="purchaseAct.php" method="post"><table border = "1">
          <tr><td>生产线类别</td><td>生产线价格</td><td>目前已有数量</td><td>目前资金还可购入</td><td>采购数量输入</td> </tr>';
foreach($equipmentlist  as $k=>$v){
    $equipmentname = $v['element_name'];
    $equipmentprice =  $v['equipment_price'];
    
    $numofequipment = 0;
    foreach($equipmentinfo as $key =>$value){
         if($value['element_id'] == $v['element_id']){
             $numofequipment++;
         } 
    }                                                                                                                                                                           
    echo'<tr><td>' . $equipmentname . '</td><td>' . $equipmentprice . '</td><td>' . $numofequipment . '</td><td>' .  floor($ttlcashequivalent / $equipmentprice) .  '</td><td><input type="text" name="' . $equipmentname. '" /></td></tr>' ;
}          
      
echo '</table>
<input type = "hidden" name="type" value="equipment">
<input type = "submit" value = "提交"></form><br/>';
break;

    case 'raw_material';
     //$rawmaterialinfo = $company->getallelements($companyid, 'raw_material');  
$rawmateriallist = $company->getelementlist('raw_material');

$rawmaterialinstock = $company->getrawmaterialinstock($companyid);
$rawmaterialontheway = $company->getrawmaterialontheway($companyid);

echo '<h4>目前可供选择的原材料有以下' . count($rawmateriallist) . '种：</h4>';
echo '<form name="raw_material" action="purchaseAct.php" method="post"><table border = "1">
          <tr><td>原材料类别</td><td>原材料价格</td><td>目前入库</td><td>目前在途</td><td>目前资金还可购入</td><td>采购数量输入</td> </tr>';
foreach($rawmateriallist  as $k=>$v){
    $rawmaterialname = $v['element_name'];
    $rawmaterialprice =  $v['raw_material_price'];
    /*
    $numofrawmaterial = 0;
    foreach($rawmaterialinfo as $key =>$value){
         if($value['element_id'] == $v['element_id']){
             $numofrawmaterial++;
         } 
    }
    */
    $numofrawmaterialinstock = 0;
    $numofrawmaterialontheway = 0;
    if(array_key_exists($rawmaterialname, $rawmaterialinstock)){
        $numofrawmaterialinstock =  $rawmaterialinstock[$rawmaterialname];
    }
    if(array_key_exists($rawmaterialname, $rawmaterialontheway)){
        $numofrawmaterialontheway =  $rawmaterialontheway[$rawmaterialname];
    }
                                                                                                                                                                              
    echo'<tr><td>' . $rawmaterialname . '</td><td>' . $rawmaterialprice . '</td><td>' . $numofrawmaterialinstock . '</td><td>' .$numofrawmaterialontheway  . '</td><td>' .  floor($ttlcashequivalent / $rawmaterialprice) .  '</td><td><input type="text" name="' . $rawmaterialname. '" /></td></tr>' ;
}          
      
echo '</table>
<input type = "hidden" name="type" value="raw_material"> 
<input type = "submit" value = "提交"></form>';
break;
              

}















?>
